import { getAuthenticationStatus } from 'services/EnterpriseCertificationListBackUpsServoce'
import userIDCardHide from 'utils/userIDCardHide'
import { Toast} from 'antd-mobile';
export default {
    namespaces: 'RealNameAuthenticationResultModel',
    state: {
        result: 'error',  //error success
        info: {
            name: '',
            cardId: ''
        }, //用户信息: 名字，身份证
        Certification: ''
    },
    reducers: {
        setResult(state, { payload }) {
            let { status} = payload
            return {
                ...state,
                result: status,
                // Certification
            }
        },
        setUserInfo(state, { payload }) {
            let { idCardNo, name } = payload
            return {
                ...state,
                info: {
                    name,
                    cardId: idCardNo
                }
            }
        }
    },
    effects: {
        //获取用户的姓名和身份证
        *getUserInfo({ payload, callback }, { call, put }) {

            let res = yield call(getAuthenticationStatus, payload)
            let { code } = res
            let reg = new RegExp(/0000$/, 'i')
            if(reg.test(code)) {

                let { enterpriseAccount: { person: { idCardNo, name } } } = res

                //隐藏身份证信息
                idCardNo = userIDCardHide(idCardNo, 5, 2)
                yield put({
                    type: 'setUserInfo',
                    payload: {
                        idCardNo,
                        name
                    }
                })
            }
            else{
              Toast.fail('获取用户信息失败！')
            }

            yield callback()
        }
    },
    subscriptions: {
        getState({ dispatch, history }) {
            history.listen(location => {
                if(location.pathname === '/RealNameAuthenticationResult') {
                    let { query:{status} } = location
                    // let {
                    //     status = 'error',
                    //     Certification = ''
                    // } = query
                    dispatch({
                        type: 'setResult',
                        payload: {
                            status,
                            // Certification
                        }
                    })
                }
            })
        }
    }
}
